# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from django.conf import settings

from datetime import datetime

class Menu(models.Model):

    name = models.CharField(u'Name', max_length=100)
    slug = models.CharField(u'Slug', max_length=100)
    site = models.ForeignKey(Site, default=settings.SITE_ID, editable=False)
    
    # Manager para gerenciar objetos do site atual
    objects = CurrentSiteManager('site')
    # Manager padrão para permitir acesso a objetos de qualquer site
    all_objects = models.Manager()

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'Menu'
        verbose_name_plural = u'Menu'
        ordering = ['name']
        unique_together = (('slug', 'site'),)

class MenuItem(models.Model):

    parent_menu = models.ForeignKey(Menu)
    parent_item = models.ForeignKey('MenuItem', null=True, blank=True)
    name = models.CharField(u'Name', max_length=100)
    link = models.CharField(u'Link', max_length=255)
    order = models.PositiveSmallIntegerField(u'Order', db_index=True, default=0)
    #This was used to only show the submenu items when it is not to display while the current menu is active (in other words, always display the item), 
    #but you can use it anyway you want
    featured = models.BooleanField(u'Featured')

    def __unicode__(self):
        return self.name

    _featured = None
    def get_featured(self):
        if self._featured is None:
            self._featured = self.menuitem_set.filter(featured=True)
        return self._featured

    _children = None
    def get_children(self):
        if self._children is None:
            self._children = self.menuitem_set.filter(featured=False)
        return self._children

    class Meta:
        verbose_name = u'Item de menu'
        verbose_name_plural = u'Item de menu'
        ordering = ['order', 'name']
